import {
	BASE_URL,
	REQUEST_SUCCESS
} from './config';

const requestHttp = ({
	url,
	method = 'GET',
	data = {},
	headers = {},
	store=undefined
}) => {
	return new Promise((resolve, reject) => {
		let token
		if(store!=undefined){
			token = store.getters['auth/token'];	
		}
		if (uni.getStorageSync('token')) {
			token = uni.getStorageSync('token')
		}
		if(token){
			headers=Object.assign({
				// 'Bearer '+
				token:token
			})
		}
		uni.request({
			url: BASE_URL + url,
			method,
			data,
			header: {
				'Content-Type': 'application/json',
				...headers
			},
			success: res => {
				if (res.statusCode === 200) {
					resolve(res.data);
				} else {
					reject(new Error(`Response statusCode is ${res.statusCode}`));
				}
			},
			fail: err => reject(err)
		});
	});
};

export default requestHttp;